﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// 像翅膀的变速子弹
/// </summary>
public class VariableGun : CurveGun
{
    int pointIndex = 0;//切换点的位置
    int flag = 1;//控制角度方向
    int waveCount = 0;//一次放两波子弹

    public override void Fire()
    {
        if (CanFire(lastFireTime))
        {
            lastFireTime = Time.time;
            fireCount = fireCount == MaxCountPerPoint ? MaxCountPerPoint + 1 : MaxCountPerPoint;

            //控制方向
            FireOnePoint(pointIndex);
            waveCount++;
            if(waveCount>=2)
            {
                waveCount = 0;
                ChangeSide();
            }
        }
    }

    void ChangeSide()
    {
        flag = -flag;
        //切换下个点
        pointIndex++;
        if (pointIndex >= firePoints.Length)
        {
            pointIndex = 0;
        }
    }

    protected override void FireOnePoint(int index)
    {
        Transform firepoint = firePoints[index];
        for (int i = 0; i < fireCount; i++)
        {
            Vector3 pos = CaculPos(firepoint);
            Quaternion rot = CaculRot(firepoint, fireCount, i);
            CreateBullet(pos, rot,i+1);
        }
    }

    protected override Quaternion CaculRot(Transform firepoint, int total, int index)
    {
        var localpoint = RoleManager.Instance.localPlayer.transform.position;
        var target = Vector3.Normalize(localpoint - firepoint.position);
        firepoint.up = target;
        return firepoint.rotation * Quaternion.Euler(0, 0, flag*(index - total / 2f) * angle / total);
    }

    public void CreateBullet(Vector3 pos, Quaternion rot,int index)
    {
        GameObject bullet = GameObject.Instantiate(bulletPrefab);
        bullet.transform.position = pos;
        bullet.transform.rotation = rot;

        bullet.GetComponent<Bullet>().Fire(bulletSpeed * index /fireCount+ bulletSpeed);
    }
}
